package com.pc.scrs.service.impl;

import com.pc.scrs.bean.po.Student;
import com.pc.scrs.bean.vo.EmailVo;
import com.pc.scrs.bean.vo.MobileVo;
import com.pc.scrs.bean.vo.StudentVo;
import com.pc.scrs.bean.vo.listVo.StudentListVo;
import com.pc.scrs.bean.vo.pageInfo.StudentListVoPageInfo;
import com.pc.scrs.dao.StudentDao;
import com.pc.scrs.service.IStudentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class StudentService implements IStudentService {

    @Resource
    private StudentDao studentDao;

    @Override
    public List<MobileVo> mobileList() {
        List<String> mobileList = studentDao.queryMobileList();
        List<MobileVo> mobileVoList = new ArrayList<>();
        for (String mobile : mobileList) {
            MobileVo mobileVo = new MobileVo();
            mobileVo.setMobile(mobile);
            mobileVoList.add(mobileVo);
        }
        return mobileVoList;
    }

    @Override
    public List<EmailVo> emailList() {
        List<String> emailList = studentDao.queryEmailList();
        List<EmailVo> emailVoList = new ArrayList<>();
        for (String email : emailList) {
            EmailVo emailVo = new EmailVo();
            emailVo.setEmail(email);
            emailVoList.add(emailVo);
        }
        return emailVoList;
    }

    @Override
    public StudentListVoPageInfo studentList(String mobile, String email, Integer sex, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Student> studentList = studentDao.queryStudentList(mobile, email, sex);
        List<StudentListVo> studentListVoList = new ArrayList<>();
        PageInfo<Student> studentPageInfo = new PageInfo<>(studentList);
        for (Student student : studentList) {
            StudentListVo studentListVo = new StudentListVo();
            studentListVo.setId(student.getId());
            studentListVo.setMobile(student.getMobile());
            studentListVo.setEmail(student.getEmail());
            studentListVo.setName(student.getName());
            studentListVo.setAge(student.getAge());
            studentListVo.setSex(student.getSex());
            studentListVo.setStatus(student.getStatus());
            studentListVo.setCreateTime(student.getCreateTime());
            studentListVoList.add(studentListVo);
        }
        StudentListVoPageInfo studentListVoPageInfo = new StudentListVoPageInfo();
        studentListVoPageInfo.setStudentListVoList(studentListVoList);
        studentListVoPageInfo.setPageNum(studentPageInfo.getPageNum());
        studentListVoPageInfo.setPageSize(studentPageInfo.getPageSize());
        studentListVoPageInfo.setPages(studentPageInfo.getPages());
        studentListVoPageInfo.setTotal(studentPageInfo.getTotal());
        return studentListVoPageInfo;
    }

    @Override
    public Integer updatePassword(Integer id, String password) {
        Integer result = studentDao.updatePassword(id, password);
        return result;
    }

    @Override
    public StudentVo queryStudentById(Integer id) {
        Student student = studentDao.queryStudentById(id);
        StudentVo studentVo = new StudentVo();
        studentVo.setId(student.getId());
        studentVo.setEmail(student.getEmail());
        studentVo.setMobile(student.getMobile());
        studentVo.setSex(student.getSex());
        studentVo.setName(student.getName());
        studentVo.setSign(student.getSign());
        studentVo.setAvatar(student.getAvatar());
        studentVo.setAge(student.getAge());
        System.out.println(studentVo);
        return studentVo;
    }

    @Override
    public Integer disableStudent(Integer id) {
        Integer result = studentDao.disableStudent(id);
        return result;
    }

    @Override
    public Integer enableStudent(Integer id) {
        Integer result = studentDao.enableStudent(id);
        return result;
    }

    @Override
    public Integer updateStudent(StudentVo studentVo) {
        Student student = new Student();
        student.setId(studentVo.getId());
        student.setEmail(studentVo.getEmail());
        student.setMobile(studentVo.getMobile());
        student.setSex(studentVo.getSex());
        student.setName(studentVo.getName());
        student.setSign(studentVo.getSign());
        student.setAvatar(studentVo.getAvatar());
        student.setAge(studentVo.getAge());
        int result = studentDao.updateByPrimaryKeySelective(student);
        return result;
    }

    @Override
    public Integer addStudent(StudentVo studentVo) {
        Student student = new Student();
        student.setId(studentVo.getId());
        student.setEmail(studentVo.getEmail());
        student.setPassword(studentVo.getPassword());
        student.setMobile(studentVo.getMobile());
        student.setSex(studentVo.getSex());
        student.setName(studentVo.getName());
        student.setSign(studentVo.getSign());
        student.setAvatar(studentVo.getAvatar());
        student.setAge(studentVo.getAge());
        int result = studentDao.insertSelective(student);
        return result;
    }
}
